textchild: Return an array from get_widgets
authorTimm Bäder <mail@baedert.org>
Sat, 25 Apr 2020 13:42:24 +0000 (15:42 +0200)
committerTimm Bäder <mail@baedert.org>
Tue, 5 May 2020 06:20:09 +0000 (08:20 +0200)
Yay, one GList less.

gtk/gtktextchild.c
gtk/gtktextchild.h
gtk/gtktextlayout.c

index 0a2e6163dd238f01ffe799eb69d6e7124e363c78..fb2b0f14c8588a0edeb88dca4f49edb7759e285e 100644 (file)
@@ -437,21 +437,31 @@ gtk_text_child_anchor_finalize (GObject *obj)
  *
  * Returns: (element-type GtkWidget) (transfer container): list of widgets anchored at @anchor
  **/
-GList*
-gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
+GtkWidget **
+gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor,
+                                   guint              *out_len)
 {
   GtkTextLineSegment *seg = anchor->segment;
-  GList *list = NULL;
+  GPtrArray *arr;
   GSList *iter;
 
   CHECK_IN_BUFFER_RETURN (anchor, NULL);
-  
+
+  g_return_val_if_fail (out_len != NULL, NULL);
   g_return_val_if_fail (seg->type == &gtk_text_child_type, NULL);
 
   iter = seg->body.child.widgets;
+
+  if (!iter)
+    {
+      *out_len = 0;
+      return NULL;
+    }
+
+  arr = g_ptr_array_new ();
   while (iter != NULL)
     {
-      list = g_list_prepend (list, iter->data);
+      g_ptr_array_add (arr, iter->data);
 
       iter = iter->next;
     }
@@ -459,7 +469,8 @@ gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor)
   /* Order is not relevant, so we don't need to reverse the list
    * again.
    */
-  return list;
+  *out_len = arr->len;
+  return (GtkWidget **)g_ptr_array_free (arr, FALSE);
 }
 
 /**
index 74375778972df4532da2c76fa07190a7697e369f..2f9cad3fd32f406315c92478c42403bec4378971 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <gdk/gdk.h>
 #include <glib-object.h>
+#include <gtkwidget.h>
 
 G_BEGIN_DECLS
 
@@ -78,7 +79,8 @@ GDK_AVAILABLE_IN_ALL
 GtkTextChildAnchor* gtk_text_child_anchor_new         (void);
 
 GDK_AVAILABLE_IN_ALL
-GList*              gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor);
+GtkWidget **        gtk_text_child_anchor_get_widgets (GtkTextChildAnchor *anchor,
+                                                       guint              *out_len);
 GDK_AVAILABLE_IN_ALL
 gboolean            gtk_text_child_anchor_get_deleted (GtkTextChildAnchor *anchor);
 
index 0719dcb222d272b326b26c55e2c4abf535d7e4fb..d07413d16a5940348737aadf9aced030f558cad3 100644 (file)
@@ -2010,8 +2010,9 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
           gint byte_index;
           GtkTextIter text_iter;
           GtkTextChildAnchor *anchor = NULL;
-          GList *widgets = NULL;
-          GList *l;
+          GtkWidget **widgets = NULL;
+          guint n_widgets = 0;
+          guint i;
 
           /* The pango iterator iterates in visual order.
            * We use the byte index to find the child widget.
@@ -2019,13 +2020,13 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
           byte_index = pango_layout_iter_get_index (run_iter);
           line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
           anchor = gtk_text_iter_get_child_anchor (&text_iter);
-         if (anchor)
-            widgets = gtk_text_child_anchor_get_widgets (anchor);
+          if (anchor)
+            widgets = gtk_text_child_anchor_get_widgets (anchor, &n_widgets);
 
-          for (l = widgets; l; l = l->next)
+          for (i = 0; i < n_widgets; i++)
             {
+              GtkWidget  *child = widgets[i];
               PangoRectangle extents;
-              GtkWidget *child = l->data;
 
               if (_gtk_anchored_child_get_layout (child) == text_layout)
                 {
@@ -2047,7 +2048,7 @@ allocate_child_widgets (GtkTextLayout      *text_layout,
                 }
             }
 
-          g_list_free (widgets);
+          g_free (widgets);
         }
     }
   while (pango_layout_iter_next_run (run_iter));